import com.ithema.web.entity.Customer;
import com.ithema.web.entity.LinkMan;
import com.ithema.web.utils.HibernateUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import org.junit.Test;

import java.util.List;
import java.util.Set;

public class test {
    @Test
    public  void  onetomany(){
        //配置文件OneToMany
        Configuration configure = new Configuration().configure();
        SessionFactory sessionFactory = configure.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

     //创建一个客户
        Customer customer = new Customer();
        customer.setCname("奥巴");

        //创建联系人
        LinkMan linkMan1 = new LinkMan();
        linkMan1.setLname("普");
        LinkMan linkMan2 = new LinkMan();
        linkMan2.setLname("习近");

        //建立关联
        customer.getLinkMan().add(linkMan1);
        customer.getLinkMan().add(linkMan2);
       // 使用配置文件是解开 注释
//        linkMan1.setCustomer(customer);
//        linkMan2.setCustomer(customer);

        //保存
//        session.save(linkMan1);
//        session.save(linkMan2);
        //级联操作 看hbm.xml配置
        session.save(customer);


        tx.commit();
        session.close();
        sessionFactory.close();
    }

    @Test
    public void   ONETOMANY(){
        //注解方式走的
        Configuration configure = new Configuration().configure();
        SessionFactory sessionFactory = configure.buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        //创建客户
//       Customer customer = new Customer();
//        customer.setCname("tom1");
//        //创建联系人
//        LinkMan linkMan = new LinkMan();
//        linkMan.setLname("lucy1");
//        LinkMan linkMan1 = new LinkMan();
//        linkMan1.setLname("jerry1");
//        //建立关系
//        customer.getLinkMan().add(linkMan);
//        customer.getLinkMan().add(linkMan1);
//        session.saveOrUpdate(customer);
////        session.save(linkMan);
////        session.save(linkMan1);
//        session.getTransaction().commit();
//        session.close();
//        sessionFactory.close();
    }
    @Test
    public void manytomany(){
        //多对多 最新版的 这样写 必须一方放弃维护主键 不然报错 
        Configuration configure = new Configuration().configure();
        SessionFactory sessionFactory = configure.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();
        
//        Customer customer =new Customer();
//        customer.setCname("jeck1");
//        
//        LinkMan linkMan = new LinkMan();
//        linkMan.setLname("tom1");
//        
//        customer.getLinkMan().add(linkMan);
//        linkMan.getCustomer().add(customer);
//        
//        session.save(customer);
//        session.save(linkMan);
//        
//        transaction.commit();
//        session.close();
    }

    @Test
    public  void  MANYTOMANY(){
        Configuration configure = new Configuration().configure();
        SessionFactory sessionFactory = configure.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();

        Customer customer =new Customer();
        customer.setCname("jeck1");

        LinkMan linkMan = new LinkMan();
        linkMan.setLname("tom1");
/*

        customer.getLinkMan().add(linkMan);
        linkMan.getCustomer().add(customer);
*/

        session.save(customer);
        session.save(linkMan);
        
        
        transaction.commit();
        session.close();
        sessionFactory.close();
    }
    
    @Test 
    public  void  quer(){
        Session session = HibernateUtils.openSession();

//        Customer customer = new Customer();
//        customer.setCname("tom");
//
//        LinkMan linkMan = new LinkMan();
//        linkMan.setLname("link");
//        LinkMan linkMan1 = new LinkMan();
//        linkMan1.setLname("lucy");
//        
//        customer.getLinkMan().add(linkMan);
//        customer.getLinkMan().add(linkMan1);
//        linkMan.setCustomer(customer);
//        linkMan1.setCustomer(customer);

        Query form_customer = session.createQuery("from Customer ");
        List<Customer> list = form_customer.list();
        for (Customer customer:list) {
            Set<LinkMan> linkMan = customer.getLinkMan();
            for (LinkMan linkMan1 :linkMan){
                System.out.println(linkMan1.getLname()+"+"+linkMan1.getLid());
            }
        }

        //HQL语句参数从0 开始
//        Query query = session.createQuery("from LinkMan where lid =? ");
//        
//        query.setParameter(0,1);
//        Object o = query.uniqueResult();

       /* //原生SQL语句 参数从1 开始
        NativeQuery nativeQuery = session.createNativeQuery("SELECT  * FROM LinkMan WHERE lid = ?");
        nativeQuery.setParameter(1,1);
        Object o = nativeQuery.uniqueResult();
        
        System.out.println(o.toString());*/
        ///session.save(customer);
        session.close();
    }
}
